package ip.command;

import ip.ejb.session.facade.LanguageFacadeLocal;
import ip.ejb.session.facade.LanguageFacadeLocalHome;
import ip.exceptions.UnexpectedException;
import ip.util.EJBHomeFactory;

import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

public class GetLanguagesCommand extends Command{

	private Collection languages;
	private LanguageFacadeLocal languageFacadeLocal;
	private Logger logger = Logger.getLogger(this.getClass());

	public void init() throws UnexpectedException{
		try {
			logger.debug("init()");
			LanguageFacadeLocalHome languageFacadeLocalHome = (LanguageFacadeLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.session.facade.LanguageFacadeLocalHome.class);
			languageFacadeLocal = languageFacadeLocalHome.create();
		} catch (NamingException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();			
		} catch (CreateException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		}

	}
	public void execute() throws UnexpectedException{
		try{
			logger.debug("execute()");
			languages = languageFacadeLocal.getAllLanguages();
			
		}
		catch (Exception e) {
			logger.error(e);
			throw new UnexpectedException();
		}
	}

	public void dispose() throws UnexpectedException{
		try {
			logger.debug("dispose()");
			languageFacadeLocal.remove();
		} catch (EJBException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		} catch (RemoveException e) {
			logger.error(e);
			e.printStackTrace();
			throw new UnexpectedException();
		}
	}

	public Collection getLanguages() {
		return languages;
	}

}
